<?php

namespace app\api\controller;

use think\Controller;
use think\Db;
use think\facade\Session;
use think\Request;
use think\facade\Cache;

/*
*还款类
*/

class Repayment extends Base
{
	/**
	 * 计划列表
	 * @Author tw
	 * @Date   2018-09-13
	 */
	public function index()
	{
		if ($this->request->isPost()) {
			$post = input('post.');
			$uid = $this->uid;//用户id
			$cid = $post['cid'];//信用卡id
			$state = isset($post['state']) ? $post['state'] : '';//计划状态
			$getdata = $where = array();
			$where['mission_uid'] = $uid;

			if ($state <> '') {

				$where['mission_state'] = $state;
			}
			if ($cid) {
				$where['mission_cid'] = $cid;
			}
			$list = Db::name('mission')->where($where)->order('mission_id desc')->paginate(10, false, ['query' => $getdata]);
			foreach ($list as $k=>&$item) {
				if($item["mission_state"] == 6){
					$item["mission_state"] = 4;
					$list[$k] = $item;
				}
			}
			if (empty($list)) {
				return json(['error' => 1, 'msg' => '没有计划']);
			}
			return json(['error' => 0, 'msg' => '成功', 'data' => $list]);
		} else {
			return json(['error' => 1, 'msg' => '非法请求']);
		}
	}

	/**
	 * 银行卡列表
	 * @Author tw
	 * @Date   2018-09-13
	 */
	public function card()
	{
		if ($this->request->isPost()) {

			$post = input('post.');
			$uid = $this->uid;//用户id
			$where['card_uid'] = $uid;
			$where['card_type'] = 1;
			$where['card_blocked'] = 0;
			$list = Db::name('user_card')->alias('c')
				->join('bank_list b', 'b.list_id=c.card_bank_id', 'LEFT')
				->where($where)
				->order('card_state desc')
				->select();
			if (empty($list)) {
				return json(['error' => 1, 'msg' => '没有信用卡']);
			}

			return json(['error' => 0, 'msg' => '成功', 'data' => $list]);
		} else {
			return json(['error' => 1, 'msg' => '非法请求']);
		}
	}

	/*
     * 获取渠道
     * 2018年9月18日23:48:52
     * 刘媛媛
     */
	public function getChannel()
	{
		if ($this->request->isPost()) {
			$bankid = input('post.bankid');


			if (empty($bankid)) {
				return json(['error' => 1, 'msg' => '参数错误']);
			}
			//->field('card_type,card_no,card_phone,card_name')

			$bank = Db::name('userCard')->where(['card_id' => $bankid])->where('card_type', 1)->where('card_uid', $this->uid)->find();
			if (!$bank) {
				return json(['error' => 1, 'msg' => '银行卡不存在']);
			}

			$bankList = Db::name('bankList')->where(['list_id' => $bank['card_bank_id']])->find();
			//$bank['bank_name'] = $bname;
			$bankList['list_logo'] = 'http://' . $_SERVER['HTTP_HOST'] . $bankList['list_logo'];
			//2018-9-21 修改
			/*$list = Db::name('paymentChannel')->where('channel_use',1)->select();

            foreach($list as $key=>$vs){
            	$payment = Db::name('payment')
            	->where('payment_channel_id',$vs['channel_id'])
            	->where('payment_type',2)
            	->where('payment_use',1)->find();
            	if(!$payment){
            		unset($list['$key']);
            	}
            }*/
			//查询支付通道
			$list = Db::name('payment')->field('payment_id,payment_name,payment_bind')->where('payment_use', 'eq', '1')->where('payment_type', 'eq', '2')->select();
			//halt($list);
			if (empty($list)) {
				return json(['error' => 1, 'msg' => '无可用通道']);
			}

			foreach ($list as $k => $v) {
				//查询通道支持银行
				if (empty(Db::name('bank')->where(['bank_bid' => $bank['card_bank_id'], 'bank_pay_id' => $v['payment_id']])->find())) {
					unset($list[$k]);
					continue;
				}

				if ($v['payment_bind'] == 0) {
					$list[$k]['state'] = 1;//可用
					$list[$k]['state_msg'] = '正常';
				} elseif ($v['payment_bind'] == 1) {
					$payment_card = Db::name('payment_card')
						->where('card_pay_id', 'eq', $v['payment_id'])
						->where('card_pay_uid', 'eq', $uid)
						->where('card_cid', 'eq', $cid)
						->where('card_type', 'eq', '1')
						->find();
					if (empty($payment_card) || $payment_card['card_state'] == 2) {
						$list[$k]['state'] = 0;//需要绑卡
						$list[$k]['state_msg'] = '需要绑卡';
						if ($v['payment_bind_way'] == 'web') {
							$list[$k]['url'] = 'http://' . $_SERVER['HTTP_HOST'] . '/api/repayment/bind_web/?pay_id=' . $v['payment_id'] . '&uid=' . $this->uid . '&cid=' . $bank['card_id'] . '&token=' . $this->token;
						} elseif ($v['payment_bind_way'] == 'api') {

							$list[$k]['url'] = 'http://' . $_SERVER['HTTP_HOST'] . '/api/repayment/bind_api';
						}
					} elseif ($payment_card['card_state'] == 3) {
						$list[$k]['state'] = 2;//绑卡中
						$list[$k]['state_msg'] = '绑卡中';
					} else {
						$list[$k]['state'] = 1;//可用
						$list[$k]['state_msg'] = '正常';
					}
				}
			}
			$list = array_values($list);
			$datas = array();
			foreach ($list as $ks => $vs) {
				$datas[] = $vs;
			}
			unset($list);
			return json(['error' => 0, 'msg' => '成功', 'data' => $datas, 'bank' => $bank, 'bankList' => $bankList]);


		} else {
			return json(['error' => 1, 'msg' => '参数错误']);
		}
	}

	/**
	 * 升级可用的笔数
	 * @return mixed
	 */
	public function requests()
	{
		if ($this->request->isPost()) {
			$post = input('post.');
			$pay_id = $post['pay_id'];//支付通道id
			$cid = $post['cid'];
			$repayAmount = $post['money'];
			$uid = $this->uid;
			$start_time = $post['start_time'];//开始时间
			$end_time = $post['end_time'];//结束时间
			//获取支付渠道配置信息


			if (empty($start_time)) {
				return json(['error' => 1, 'msg' => '开始日期必填填写']);
			}
			if (empty($end_time)) {
				return json(['error' => 1, 'msg' => '结束日期必填填写']);
			}
			//开始时间不能小于当天时间
			if (strtotime(date('Y-m-d', time())) > strtotime($start_time)) {
				return json(['error' => 1, 'msg' => '开始日期不能小于当天日期']);
			}
			//结束日期必须大于开始日期
			if (strtotime($start_time) > strtotime($end_time)) {
				return json(['error' => 1, 'msg' => '结束日期必须大于开始日期']);
			}

			//获取总天数如果开始时间是今天同时下午6点以后的计划那么开始时间延迟一天
			$start_time_mew = $start_time;
			if (date("H") >= 18 && date("Y-m-d", strtotime("$start_time")) == date("Y-m-d")) {
				$start_time_mew = date("Y-m-d", strtotime("+1 day", strtotime("$start_time")));
			}
			//总天数
			$day_count = diffBetweenTwoDays($start_time_mew, $end_time);

			//查询通道信息
			$payment = Db::name('payment')
				->where('payment_id', $pay_id)
				->where('payment_type', 2)
				->where('payment_use', 1)
				->find();
			if (!$payment) {
				return json(['error' => 1, 'msg' => '支付通道不存在']);
			}

			//通道是否需要绑卡
			/*if($payment['payment_bind']==1)
			{
				$payment_card = Db::name('payment_card')->where(['card_pay_id'=>$pay_id,'card_cid'=>$cid,'card_type'=>'1','card_state'=>'1'])->find();
				if(empty($payment_card))
				{
					return json(['error'=>1,'msg'=>'未绑定通道']);
				}
			}*/


			//获取卡信息
			$userBank = Db::name('userCard')
				->where('card_id', $cid)
				->where('card_blocked', 0)
				->where('card_uid', $uid)
				->find();
			if (!$userBank) {
				return json(['error' => 1, 'msg' => '银行卡不存在']);
			}
			if ($userBank['card_type'] != 1) {
				return json(['error' => 1, 'msg' => '额..只支持信用卡哦']);
			}

			//这里本来限制总还款金额，这里不再限制
//			if ($repayAmount > $payment['paymentst_money']) {
//				return json(['error' => 1, 'msg' => '最高计划金额为' . $payment['paymentst_money']]);
//			}

			//查询出还款费率
			$rate = Db::name('userRate')->where(['rate_uid' => $this->uid, 'rate_type' => 1])->find();
			if (!$rate) {
				return json(['error' => 1, 'msg' => '费率错误']);
			}
			if ($pay_id == '7') {
				$user_type_id = Db::name("user")->where(["user_id" => $uid])->value("user_type_id");
				$rate["rate_rate"] = (new \app\pay\model\User())->getUserRankConfig($user_type_id, $pay_id);
			}


			//判断支付通道或者渠道是否支持此卡暂时没写
			$bank = Db::name('bank')->where(['bank_bid' => $userBank['card_bank_id'], 'bank_pay_id' => $payment['payment_id']])->find();
			if (empty($bank)) {
				return json(['error' => 1, 'msg' => '通道不支持此银行']);
			}

			//获取允许最小金额
			if (!empty($bank['bank_min_money']) && $bank['bank_min_money'] >= $payment['payment_min_money'] && $bank['bank_min_money'] != '0.00') {
				$min_money = $bank['bank_min_money'];
			} else {
				$min_money = $payment['payment_min_money'];
			}
			//获取允许最大金额
			if (!empty($bank['bank_max_money']) && $bank['bank_max_money'] <= $payment['payment_max_money'] && $bank['bank_max_money'] != '0.00') {
				$max_money = $bank['bank_max_money'];
				//考虑单日限额1000 单笔500 那么一天最多执行2笔
				if($max_money==300){
					$payment["payment_day_num"]=4;
				}
			} else {
				$max_money = $payment['payment_max_money'];
			}


			//开始生成计划任务算法
			//计算平均数取整
			$list = array();
			//gaoyuan不再使用该方法
			if ($payment['payment_mode'] == 1) {//多刷一还
				$min_money = $min_money * $payment['payment_pattern'];
				//$max_money = $max_money * $payment['payment_pattern'];
			}
			$interval_time = $payment['payment_interval_time'] * 60;//最低计划间隔时间
			$single_time = (($interval_time + 60 * 10) / 60 / 60); //单个计划执行所需时间单位小时
			$day_max_num = floor(9 / $single_time);     //每天最大笔数（包含刷还）
			$count = floor($day_max_num / ($payment['payment_pattern'] + 1));//每天笔数


			//检查计算出的笔数是否超过了通道设定的每天最大笔数
			if ($count * $day_count < $payment['payment_num']) {
				$payment['payment_num'] = $count * $day_count;
			}


			//根据数据库笔计算数量
			for ($i = $payment['payment_num']; $i > 0; $i--) {
				if ($max_money != 0) {
					if ($repayAmount / $i > $max_money) {
						continue;
					}
				}

				if ($min_money != 0) {
					if ($repayAmount / $i < $min_money) {
						continue;
					}
				}
				$list[] = $i;//$this->dispersed($i,$repayAmount,$payment['payment_min_money'],$payment['payment_max_money'],$payment['payment_risk_start'],$payment['payment_risk_end'],$repayAmount/$i);
			}

			if (count($list) == 0) {
				return json(['error' => 1, 'msg' => '此金额无法生成计划']);
			}
			//减少笔数
			if (count($list) > 6) {
				foreach ($list as $ks => $v) {
					if ($ks == 0 && ($repayAmount / $v) == $min_money && count($list) > 1) {
						unset($list[$ks]);
					}
					if ($ks % 4 != 0 and $ks != 1 and $ks != count($list) - 1) {
						unset($list[$ks]);
					}
				}
			}
			foreach ($list as $v) {
				$fre = round(($repayAmount * $rate['rate_rate']) + ($v * $rate['rate_close_rate']), 2);
				$data[] = [
					'sum' => round(($repayAmount) / $v + $fre, 2) . '以预览计划为准',
					'muber' => $v,
					'fee' => $fre
				];
			}
			return json(['error' => 0, 'msg' => '请求成功', 'data' => $data]);
		}
	}


	/**
	 * 预览计划 2.0
	 * @Author tw
	 * @return [type] [description]
	 */
	public function preview()
	{
		$post = input('post.');
		if (empty($post)) {
			return json(['error' => 1, 'msg' => '预览计划失败,请重试!']);
		}
		$uid = $this->uid;
		$pay_id = $post['pay_id'];//渠道id
		$cid = $post['cid'];//信用卡id
		$repayAmount = $post['money'];//还款或者自定义金额
		$type = intval($post['type']);//还款笔数
		$version = $post['version'];//app版本
		$region = $post['region'];//地区
		$city_id = $post['city_id'];//城市id
		$start_time = $post['start_time'];//开始时间
		$end_time = $post['end_time'];//结束时间
		//缓存ID
		$cacheName = md5($pay_id . $cid . $repayAmount . $uid);


		/*******************检查数据开始************************/
		if ($version != '2.0') {
			return json(['error' => 1, 'msg' => '请联系客服或更新app']);
		}
		if (empty($repayAmount)) {
			return json(['error' => 1, 'msg' => '还款金额必填填写']);
		}
		if (empty($start_time)) {
			return json(['error' => 1, 'msg' => '开始日期必填填写']);
		}
		if (empty($end_time)) {
			return json(['error' => 1, 'msg' => '结束日期必填填写']);
		}
		//开始时间不能小于当天时间
		if (strtotime(date('Y-m-d', time())) > strtotime($start_time)) {
			return json(['error' => 1, 'msg' => '开始日期不能小于当天日期']);
		}
		//结束日期必须大于开始日期
		if (strtotime($start_time) > strtotime($end_time)) {
			return json(['error' => 1, 'msg' => '结束日期必须大于开始日期']);
		}


		//获取总天数如果开始时间是今天同时下午6点以后的计划那么开始时间延迟一天
		$start_time_mew = $start_time;
		if (date("H") >= 12 && date("Y-m-d", strtotime("$start_time")) == date("Y-m-d")) {
			$start_time_mew = date("Y-m-d", strtotime("+1 day", strtotime("$start_time")));
		}
		//总天数
		$day_count = diffBetweenTwoDays($start_time_mew, $end_time);
		if ($day_count <= 0) {
			return json(['error' => 1, 'msg' => '选择的还款日期范围错误']);
		}


		//获取渠道信息
		$payment = Db::name('payment')
			->where('payment_id', $pay_id)
			->where('payment_type', 2)
			->where('payment_use', 1)
			->find();
		if (!$payment) {
			return json(['error' => 1, 'msg' => '请选择支付通道']);
		}
		if (!in_array($payment['payment_mode'], [1, 2])) {
			return json(['error' => 1, 'msg' => '请联系管理员,升级配置']);
		}


		//商户注册
		$payment_user = Db::name('payment_user')->where('user_pay_id', $pay_id)->where('user_uid', $uid)->where('user_type', 1)->find();
		if (empty($payment_user) || $payment_user['user_state'] == 0 || $payment_user['user_state'] == 2) {
			$res = controller('pay/' . $payment['payment_controller'])->register($pay_id, $uid, $cid);
			if ($res['error'] == '1') {
				return json($res);
			}
		}

		//后台自定义笔数
		if ($payment['payment_pattern'] == 0) {
			$pattern = 1;
		} else {
			$pattern = $payment['payment_pattern'];
		}

		//获取卡信息
		$userBank = Db::name('userCard')->where('card_id', $cid)->where('card_blocked', 0)->where('card_uid', $this->uid)->find();
		if (!$userBank) {
			return json(['error' => 1, 'msg' => '银行卡不存在']);
		}
		if ($userBank['card_type'] != 1) {
			return json(['error' => 1, 'msg' => '额..只支持信用卡哦']);
		}
		$payment_bank = Db::name('bank')->where(['bank_bid' => $userBank['card_bank_id'], 'bank_pay_id' => $pay_id])->find();
		if (empty($payment_bank)) {
			return json(['error' => 1, 'msg' => '不支持此银行卡']);
		}

		$rate = Db::name('userRate')->where('rate_uid', $this->uid)->where('rate_type', 1)->find();
		if (!$rate) {
			return json(['error' => 1, 'msg' => '会员费率不存在']);
		}
		if ($pay_id == '8') {
			$user_type_id = Db::name("user")->where("user_id", $uid)->value("user_type_id");
			$rate["rate_rate"] = (new \app\pay\model\User())->getUserRankConfig($user_type_id, $pay_id);
		}
		$rate["rate_close_rate"] = 2;


		//halt($rate);
		//最高计划金额;gaoyuan这里不再检查
		//if ($repayAmount > $payment['paymentst_money']) {
		//return json(['error' => 1, 'msg' => '最高计划金额为' . $payment['paymentst_money']]);
		//}

		//根据银行卡和通道获取允许最小金额
		if (!empty($payment_bank['bank_min_money']) && $payment_bank['bank_min_money'] != '0.00' && $payment_bank['bank_min_money'] >= $payment['payment_min_money']) {
			$min_money = $payment_bank['bank_min_money'];
		} else {
			$min_money = $payment['payment_min_money'];
		}
		//根据银行卡和通道获取允许最大金额
		if (!empty($payment_bank['bank_max_money']) && $payment_bank['bank_max_money'] != '0.00' && $payment_bank['bank_max_money'] <= $payment['payment_max_money']) {
			$max_money = $payment_bank['bank_max_money'];
		} else {
			$max_money = $payment['payment_max_money'];
		}

		//如果是一刷的情况下  最小值不用处理
		if ($payment['payment_mode'] == 1) {
			$min_money = $min_money / $payment['payment_pattern'];
		}


		if ($payment['payment_mode'] == 1) {
			$min_money = $min_money * $payment['payment_pattern'];
		}
		if ($repayAmount < $min_money) {
			return json(['error' => 1, 'msg' => '金额不能低于' . $min_money]);
		}


		//检查账单笔数是否合理
		$interval_time = $payment['payment_interval_time'] * 60;//最低计划间隔时间
		$interval_time_end = $interval_time + 60 * rand(5, 10);
		$single_time = (($interval_time + 60 * 10) / 60 / 60); //单个计划执行所需时间
		//halt($payment["payment_day_num"]);
		$day_max_num = $payment["payment_day_num"] / ($pattern + 1);//floor(9 / $single_time);//根据时间间隔计算出每天最大数
		//获取最大笔数
		$maxNum = 0;
		$minNum = 0;
		$maxNumByDays = $day_max_num * $day_count;
		$maxNumByMoney = (int)($repayAmount / ($min_money * $pattern));//halt($maxNumByMoney);
		if (($repayAmount / ($min_money * $pattern) > 400 && $repayAmount / ($min_money * $pattern) < 600) && ($maxNumByMoney >= 10)) {
			$maxNumByMoney = $maxNumByMoney - 2;
		}
		if ($maxNumByMoney > $maxNumByDays) {
			$maxNum = $maxNumByDays;
		} else {
			$maxNum = $maxNumByMoney;
		}
		//halt($maxNum);
		//获取最小笔数
		$minNum = ceil($repayAmount / ($max_money * $pattern));

		//根据选择的还款笔数判断金额是否合理
		if ($type == 0) {//如果没有选择还款笔数
			$rand_money = rand($min_money, $max_money);
			$num = ceil($repayAmount / $rand_money);//随机生成笔数
		} else {//如果有选择还款笔数
			if ($payment['payment_max_money'] != 0) {
				if ($type < $minNum) {
					return json(['error' => 1, 'msg' => '此笔数低于最小值,最小笔数为' . ceil($minNum)]);
				}
			}
			if ($payment['payment_min_money'] != 0) {
				if ($type > $maxNum) {
					return json(['error' => 1, 'msg' => '此笔数超过最大笔数,最大笔数为' . (int)$maxNum]);
				}
			}
			$num = $type;
		}

		//检查日期范围是否符合要求：至少需要几天完成计划
		if (($type % $day_max_num) > 0) {
			$day_min = (int)($type / $day_max_num + 1);
		} else {
			$day_min = ($type / $day_max_num);
		}


		if ($day_min > $day_count) {
			// $day_min ++;
			$end_day = date('Y-m-d', strtotime("$start_time_mew + $day_min day"));
			return json(['error' => 1, 'msg' => '建议设置计划结束日期' . $end_day . '或之后完成计划']);
		}
		/*******************检查数据结束************************/


		/**********************开始账单业务逻辑**************************/
		// if(($num/$payment['payment_day_num']) > $day_count){
		// 	$end_day = ceil($num/$payment['payment_day_num']);
		// 	$end_day = date('Y-m-d',strtotime("$start_time_mew + $end_day day"));
		// 	return  json(['error'=>1,'msg'=>'还款结束日期大约在'.$end_day.'完成']);
		// }

		//根据选择笔数生成还款账单金额
		if ($num == 1) {
			$lists[0] = $repayAmount;
		} else {
			//如果1000选择2笔换完那么这是一个临界值
			if ($min_money == ($repayAmount / $num) || $max_money == ($repayAmount / $num)) {
				$i = 1;
				while ($i <= $num) {
					$lists[] = $repayAmount / $num;
					$i++;
				}
			} else {
				$lists = randnum_new($repayAmount, $num, $min_money * $pattern, $max_money * $pattern, $payment['payment_risk_start'], $payment['payment_risk_end'], true);
			}
		}
		$reserved_money = max($lists);//需要保留的金额


		//计算每天几笔
		$day_num = $type * ($pattern + 1);//halt($day_num);
		if ($day_num > $payment['payment_day_num']) {
			$day_num = $payment['payment_day_num'];
		}
		//计算几天还完
		$need_day = $day_count;
		if ($type <= $day_count) {
			$need_day = $type;
		} else {
			$need_day = $day_count;
		}
		//生成那几天安排还款的时间列表
		$list_time = get_time_scope($start_time_mew, $end_time, $need_day);
		//生成每天刷还次数列表
		$list_day_num = repayment_day_count($list_time, $day_max_num, $pattern, $num);


		//生成首单消费时间
		$time = date("Y-m-d", strtotime($list_time[0]));
		if ($time == date("Y-m-d") && date('H') >= 9 && date('H') < 18) {
			$time = date("Y-m-d H:i:s", time() + rand(600, 1200));
		} else {
			$time = date("Y-m-d H:i:s", strtotime(date("Y-m-d 09:00:00", strtotime($time))) + rand(0, $interval_time_end));
		}
		//测试时间记得删除
		//$time = date("Y-m-d H:i:s", time() + rand(600, 1200));


		if ($payment['payment_region'] == 1) {
			if (empty($region)) {
				$user_card = Db::name('user_card')->where(['card_blocked' => 0, 'card_uid' => $uid, 'card_id' => $cid])->find();
				$region = trim($user_card['card_province']) . '-' . trim($user_card['card_city']) . '-' . trim($user_card['card_district']);
			}
			$region_arr = explode('-', $region);
			if ($payment['payment_mcc'] == 1) {
				if ($city_id) {
					$mcc_data = controller('pay/' . $payment['payment_controller'])->mcc($pay_id, $uid, $cid, $city_id);
				} else {
					$mcc_data = controller('pay/' . $payment['payment_controller'])->query_city_mcc($pay_id, $uid, $cid, $region_arr[0], $region_arr[1]);
				}
			} else {
				$mcc_list = Db::name('payment_mcc')
					->field('mcc_mcc as mcc,mcc_title as name')
					//->where('mcc_pay_id', $pay_id)
					->select();
				$mcc_data = ['error' => 0, 'msg' => '成功', 'data' => $mcc_list];
			}
			if ($mcc_data['error'] == '0') {
				$mcc_data = $mcc_data['data'];
				$mcc_data_count = count($mcc_data) - 1;
			}
		}
		$mission_consume = 0;//消费笔数
		$mission_repayment = 0;//还款笔数

		$interval_time_s = $interval_time;
		$interval_time_end_s = $interval_time_end;
		$paytime = '';//计划首次支付时间
		$i = 1;
		/********************************这里开始加入你的代码************************************/
		//计算每刷一笔的手续费
		$totalRate = blxsd($repayAmount * $rate["rate_rate"],1);//计算本次总费率
		$caclTotalRate = 0;//每次循环叠加费率
		$total_money=0;//消费总金额

		//halt($lists);
		foreach ($lists as $key => $money) {
			$fee2 = 0;//提现费率
			$payment['payment_day_num'] = $list_day_num[array_search(date("Y-m-d", strtotime($time)), $list_time)];
			$interval_time_end = (9 / (($interval_time_s / 60 / 60) * $payment['payment_day_num']) * 60 * 60);
			if ($interval_time < $interval_time_s) {
				$interval_time = $interval_time_s;
			}
			if ($interval_time_end < $interval_time_end_s) {
				$interval_time_end = $interval_time_end_s;
			}

			$more_money = 0;//多的金额
			//还款模式
			if ($payment['payment_mode'] == 1) {
				//多刷一还
				$moneys = randnum_new($money, $pattern, $min_money, $max_money, $payment['payment_risk_start'], $payment['payment_risk_end'], true);
				//return json(['error' => 1, 'msg' => json_encode($moneys) . $min_money]);
				foreach ($moneys as $k => $v) {
					$fee2 = 0;//提现费率
					if ($v < ($min_money)) {
						return json(['error' => 1, 'msg' => '消费金额小于最小金额；生成账单失败，请重新尝试！']);
					}
					if ($mcc_data) {
						$mcc_arr = $mcc_data[rand(0, $mcc_data_count)];
						$mcc = $mcc_arr['mcc'];
						$mcc_name = $mcc_arr['name'];
					}


					//计算费率
					//var_dump($v);
					$fee = round($v*$rate["rate_rate"],1);
					if ($mission_consume == $num * $pattern - 1) {
						$syfee = $totalRate-$caclTotalRate;
						if($syfee>$fee){
							$fee = $syfee;
						}
					}else{
						$caclTotalRate += $fee;
					}
					//var_dump($fee);



					//在第一次的时候插入一个还款手续费进去
					if ($k == 0) {
						$fee2 = $rate["rate_close_rate"];
					}

					$plan['money'] = (string)$v;
					$plan['time'] = (string)$time;
					$plan['type'] = '2';
					$plan['fee'] = (string)$fee;
					$plan['fee2'] = $fee2;//还款手续费
					$plan['sort'] = (string)($key + 1);
					$plan['sum_money'] = (string)($plan['money'] + $plan['fee'] + $fee2);
					$plan['mcc'] = $mcc;
					$plan['mcc_name'] = $mcc_name;
					$plan['i'] = $i;
					$list[$key][] = $plan;
					if ($key == 0 && $k == 0) {
						$paytime = $time;
					}


					$repayment_time = repayment_time($time, $i, $key + 1, $payment, $interval_time, $interval_time_end, 2, $list_time);
					$time = $repayment_time['time'];
					if (date("Y", strtotime($time)) == '1970') {
						return json(['error' => 1, 'msg' => '计划运算出错.']);
					}

					$total_money += $plan['sum_money'];//总消费金额
					unset($plan);
					$mission_consume++;//消费笔数
				}


				$plan['money'] = (string)$money;
				$plan['time'] = (string)$time;
				$plan['type'] = '1';
				$plan['fee'] = $rate["rate_close_rate"];
				$plan['fee2'] = 0;
				$plan['sort'] = (string)($key + 1);
				$plan['i'] = $i;
				$list[$key][] = $plan;
				unset($plan);
				$mission_repayment++;//还款笔数
			}


			//模式二 一刷多还
			elseif ($payment['payment_mode'] == 2) {
				if ($payment['payment_pay_mode'] == 0) {
					$fee = $rate['rate_rate'] * $money + $rate['rate_close_rate'] / $pattern;
				} elseif ($payment['payment_pay_mode'] == 1) {
					$fee = $rate['rate_rate'] * $money + $rate['rate_close_rate'] / $pattern;
					$fee = round($fee, 2);
					$fee = repayment_fee($money, $fee, $rate['rate_rate'], $rate['rate_close_rate'] / $pattern);
				}

				$plan['money'] = (string)$money;
				$plan['time'] = (string)$time;
				$plan['type'] = '2';
				$plan['fee'] = (string)round($fee, 2);
				$plan['sort'] = (string)($key + 1);
				$plan['sum_money'] = (string)($plan['money'] + $plan['fee']);
				$list[$key][] = $plan;

				// $time = date("Y-m-d H:i:s",strtotime($time) + rand($interval_time,$interval_time_end)); //时间
				$repayment_time = repayment_time($time, $i, $key + 1, $payment, $interval_time, $interval_time_end, 2, $list_time);
				$time = $repayment_time['time'];
				$i = $repayment_time['i'];
				if (date("Y", strtotime($time)) == '1970') {
					return $this->preview();
				}

				$free = $free + $plan['fee'];//总手续费
				$total_money = $total_money + $plan['sum_money'];//总消费金额
				unset($plan);

				$mission_consume++;//消费笔数

				$moneys = randnum_new($money, $pattern, $min_money, $max_money, $payment['payment_risk_start'], $payment['payment_risk_end'], true);
				foreach ($moneys as $k => $v) {
					$plan['money'] = (string)$v;
					$plan['time'] = (string)$time;
					$plan['type'] = '1';
					$plan['fee'] = '0';
					$plan['sort'] = (string)($key + 1);
					$list[$key][] = $plan;
					unset($plan);
					$mission_repayment++;//还款笔数
				}
				// $time = date("Y-m-d H:i:s",strtotime($time) + rand($interval_time,$interval_time_end)); //时间
			}

			$pre_time = $time;
			$pre_i = $i;
			$repayment_time = repayment_time($time, $i, $key, $payment, $interval_time, $interval_time_end, '1', $list_time);
			$time = $repayment_time['time'];
			if (date("Y", strtotime($time)) == '1970') {
				return json(['error' => 1, 'msg' => '计划运算出错,请重新尝试生成计划！']);
			}

			if (strtotime(date("Y-m-d", strtotime($time))) > strtotime($end_time)) {
				return json(['error' => 1, 'msg' => '请延长结束日期']);
			}

		}
		//halt("asdf");
		/********************************这里结束加入你的代码************************************/
		if ($region == '-') {
			$region = '';
		}

		$sumMoneyfree = ceil($reserved_money + $totalRate + $num * $rate["rate_close_rate"]);
		$cachedata = [
			'error' => 0,
			'msg' => '成功',
			'sumMoneyfree' => $sumMoneyfree,//预留金额(本金+总费率)
			'total_money' => $total_money,//总消费金额
			'sumMoney' => $repayAmount,//还款金额
			'free' => $totalRate,//消费产生的总手续费
			'daiSumMoney' => $num * $rate["rate_close_rate"],//还款手续费
			'start_time' => date("Y-m-d", strtotime($start_time)),//开始时间
			'end_time' => date("Y-m-d", strtotime($end_time)),//结束时间
			'paytime' => $paytime,//执行时间
			'region' => $region,//地区
			'mission_consume' => $mission_consume,//消费笔数
			'mission_repayment' => $mission_repayment,//还款笔数
			'data' => $list,
		];

		Cache::set($cacheName, $cachedata, 1200);
		return json($cachedata);
	}

	/**
	 * 编辑预览计划 行业信息
	 * @Author tw
	 * @return [type] [description]
	 */
	public function preview_edit()
	{
		$post = input('post.');
		if (empty($post)) {
			return json(['error' => 1, 'msg' => '预览计划失败,请重试!']);
		}
		$uid = $this->uid;
		$pay_id = $post['pay_id'];
		$cid = $post['cid'];
		$repayAmount = $post['money'];//还款或者自定义金额
		$type = intval($post['type']);//0是自定义金额 其他为笔数

		$cachedata = $post['data'];//json缓存数据
		if ($cachedata['error'] != 0) {
			return json(['error' => 1, 'msg' => '更改错误,请重试']);
		}
		$cacheName = md5($pay_id . $cid . $repayAmount . $uid);//缓存用
		Cache::set($cacheName, $cachedata, 1200);
		return json($cachedata);
	}

	/**
	 * 获取通道支持的省市
	 * @Author tw
	 * @return [type] [description]
	 */
	public function city()
	{
		$post = input('post.');
		if (empty($post)) {
			return json(['error' => 1, 'msg' => '获取地区失败']);
		}
		$id = $post['id'];//城市id 默认0
		$uid = $this->uid;//用户id
		$pay_id = $post['pay_id'];//支付通道id


		$payment_controller = Db::name('payment')->where('payment_id', $pay_id)->value('payment_controller');
		if (empty($payment_controller)) {
			return json(['error' => 1, 'msg' => '通道不存在']);
		}

		$result = controller('pay/' . $payment_controller)->city($pay_id, $uid, $id);
		return json($result);
	}

	/**
	 * 获取所有通道支持所有省市
	 * @Author tw
	 * @return [type] [description]
	 */
	public function city_all()
	{
		$post = input('post.');
		if (empty($post)) {
			return json(['error' => 1, 'msg' => '获取地区失败']);
		}
		$id = $post['id'];
		$uid = $this->uid;//用户id
		$pay_id = $post['pay_id'];//支付通道id


		$payment_controller = Db::name('payment')->where('payment_id', $pay_id)->value('payment_controller');
		if (empty($payment_controller)) {
			return json(['error' => 1, 'msg' => '通道不存在']);
		}

		$result = controller('pay/' . $payment_controller)->city_all($pay_id, $uid, $id);
		return json($result);
	}

	/**
	 * 生成计划
	 * @Author tw
	 * @Date   2018-09-25
	 * @return [type]     [description]
	 */
	public function create()
	{
		$post = input('post.');
		if (empty($post)) {
			return json(['error' => 1, 'msg' => '请求参数错误']);
		}
		$uid = $this->uid;//用户id
		$pay_id = $post['pay_id'];//支付通道id
		$cid = $post['cid'];//银行卡id
		$repayAmount = $post['money'];//还款或者自定义金额
		$type = intval($post['type']);//0是自定义金额 其他为笔数
		$version = $post['version'];//版本号
		$region = $post['region'];//地区

		if ($region == '-') {
			$region = '';
		}
		$city_id = $post['city_id'];//城市id
		$start_time = $post['start_time'];//开始时间
		$end_time = $post['end_time'];//结束时间
		if ($version != '2.0') {
			return json(['error' => 1, 'msg' => '请联系客服或更新app']);
		}
		$payment = Db::name('payment')
			->where('payment_id', $pay_id)
			->where('payment_type', 2)
			->where('payment_use', 1)
			->find();
		if (!$payment) {
			return json(['error' => 1, 'msg' => '请选择支付通道']);
		}

		//商户注册
		$payment_user = Db::name('payment_user')->where('user_pay_id', $pay_id)->where('user_uid', $uid)->where('user_type', 1)->find();
		if (empty($payment_user) || $payment_user['user_state'] == 0 || $payment_user['user_state'] == 2) {
			$res = controller('pay/' . $payment['payment_controller'])->register($pay_id, $uid, $cid);

			if ($res['error'] == '1') {
				return json($res);
			}
		}
		//通道是否需要绑卡
		if ($payment['payment_bind'] == 1) {
			$payment_card = Db::name('payment_card')->where(['card_pay_id' => $pay_id, 'card_cid' => $cid, 'card_type' => '1', 'card_state' => '1'])->find();
			if (empty($payment_card) || $payment_card['card_state'] != 1) {
				if ($payment['payment_bind_way'] == 'web') {
					$url = 'http://' . $_SERVER['HTTP_HOST'] . '/api/repayment/bind_web/?pay_id=' . $pay_id . '&uid=' . $this->uid . '&cid=' . $cid . '&token=' . $this->token;
				} elseif ($payment['payment_bind_way'] == 'api') {
					$url = 'http://' . $_SERVER['HTTP_HOST'] . '/api/repayment/bind_api';
				} else {
					return json(['error' => 1, 'msg' => '绑卡参数错误']);
				}


				//大额通道部分卡需要短信验证
				$bank = Db::name("user_card")->alias("u")
					->join("bank_list b","u.card_bank_id = b.list_id","LEFT")
					->where("u.card_id","eq",$cid)
					->field("b.list_name")
					->find();
				if($payment["payment_id"]=='8'){
					if(in_array($bank["list_name"],array("农业银行", "工商银行","光大银行","北京银行","上海银行"))){
						$payment['payment_bind_way'] = "web";
						$url = 'http://' . $_SERVER['HTTP_HOST'] . '/api/repayment/bind_web/?pay_id=' . $pay_id . '&uid=' . $this->uid . '&cid=' . $cid . '&token=' . $this->token;
					}
				}
				//halt($url);
				return json(['error' => '10', 'msg' => '银行验证绑卡信息', 'type' => $payment['payment_bind_way'], 'title' => '绑卡验证', 'url' => $url]);
			}
		}

		if (empty($repayAmount)) {
			return json(['error' => 1, 'msg' => '还款金额必填填写']);
		} elseif (empty($start_time)) {
			return json(['error' => 1, 'msg' => '开始日期必填填写']);
		} elseif (empty($end_time)) {
			return json(['error' => 1, 'msg' => '结束日期必填填写']);
		}
		//开始时间不能小于当天时间
		if (strtotime(date('Y-m-d', time())) > strtotime($start_time)) {
			return json(['error' => 1, 'msg' => '开始日期不能小于当天日期']);
		}
		//结束日期必须大于开始日期
		if (strtotime($start_time) > strtotime($end_time)) {
			return json(['error' => 1, 'msg' => '结束日期必须大于开始日期']);
		}
		$cacheName = md5($pay_id . $cid . $repayAmount . $uid);//缓存用
		$retCache = Cache::get($cacheName);
		//halt($retCache);
		if (empty($retCache)) {
			return json(['error' => 1, 'msg' => '请求超时,请重新生成计划,错误代码[' . $pay_id . '-' . $uid . '-' . $cid . ']']);
		}


//		$state = Db::name('user_card')->where('card_id', $cid)->where('card_type', 1)->find();
//		if ($state['card_state'] == 1) {
//			if (Db::name('mission')->where('mission_cid', $cid)->where('mission_state', 1)->where('mission_del', 0)->find()) {
//				return json(['error' => 1, 'msg' => '已有计划,不能重复生成!']);
//			}
//		}

		//修复不要几个计划同时跑
		$mission = Db::name('mission')
			->where("mission_cid='{$cid}' and mission_pay_id='{$pay_id}' and mission_uid='{$uid}' and mission_state in ('1','3') and mission_del='0'")
			//->where(['mission_cid'=> $cid,"mission_pay_id"=>$pay_id,"mission_state"=>1,"mission_uid"=>$uid])
			//->whereIn("mission_state",["1","3"])
			//->fetchSql(1)
			->find();
		//halt($mission);
		if(!empty($mission)){
			return json(['error' => 1, 'msg' => '已有计划,不能重复生成!']);
		}


		$rate = Db::name('userRate')->where('rate_uid', $this->uid)->where('rate_type', 1)->find();
		if (!$rate) {
			return json(['error' => 1, 'msg' => '会员费率不存在']);
		}
		$list = $retCache['data'];//还款集合
		$totalfee = $retCache['free'] + $retCache["daiSumMoney"];//手续费
		// $total_money = $retCache['total_money'];//总金额
		$mission_consume = $retCache['mission_consume'];//消费笔数
		$mission_repayment = $retCache['mission_repayment'];//还款笔数
		$paytime = $retCache['paytime'];//执行时间

		$this->payment_logs($post, $retCache, 'test');
		$start_time = date("Y-m-d", strtotime($start_time));
		$end_time = date("Y-m-d", strtotime($end_time));
		if ($retCache['start_time'] != $start_time) {
			return json(['error' => 1, 'msg' => '计划开始时间不匹配,请重新生成计划']);
		} elseif ($retCache['end_time'] != $end_time) {
			return json(['error' => 1, 'msg' => '计划结束时间不匹配,请重新生成计划']);
		} elseif ($retCache['region'] != $region) {
			$region = $retCache['region'];
			//return json(['error'=>1,'msg'=>'计划地址不匹配,请重新生成计划'.$retCache['region']]);
		}

		$mission['mission_uid'] = $uid;
		$mission['mission_cid'] = $cid;
		$mission['mission_pay_id'] = $pay_id;
		$mission['mission_state'] = 1;
		$mission['mission_type'] = 0;
		$mission['mission_form_no'] = 'CU' . date('YmdHis', time()) . $uid . rand(1000, 9999);
		$mission['mission_money'] = $repayAmount;
		$mission['mission_repayment'] = $mission_repayment;
		$mission['mission_consume'] = $mission_consume;
		$mission['mission_fee'] = $totalfee;
		$mission['mission_rate'] = $rate['rate_rate'];
		$mission['mission_close_rate'] = $rate['rate_close_rate'];
		$mission['mission_start_time'] = $start_time;
		$mission['mission_end_time'] = $end_time;
		$mission['mission_pay_time'] = $paytime;
		$mission['mission_time'] = date('Y-m-d H:i:s');
		$mission['mission_region_id'] = $region;
		$mission['mission_mcc'] = $city_id;
		$mission['mission_flag'] = $payment['payment_pattern'];
		$mission['mission_close'] = 1; //1允许用户删除
		$mission_id = Db::name('mission')->insertGetId($mission);
		if (empty($mission_id)) {
			return json(['error' => 1, 'msg' => '计划生成失败']);
		}
		foreach ($list as $key => $plan_all) {
			$oids = array();
			foreach ($plan_all as $k => $v) {
				$data['plan_uid'] = $uid;
				$data['plan_mid'] = $mission_id;
				$data['plan_money'] = $v['money'];
				$data['plan_type'] = $v['type'];
				$data['plan_pay_time'] = $v['time'];
				$data['plan_fee'] = $v['fee'];
				$data['plan_fee2'] = $v['fee2'] > 0 ? $v['fee2'] : 0;
				$data['plan_sort'] = $v['sort'];
				$data['plan_pay_id'] = $pay_id;

				if ($v['type'] == 2) {
					$data['plan_mcc'] = $v['mcc'];
					$data['plan_mcc_name'] = $v['mcc_name'];
					$data['plan_form_no'] = get_order_sn('P', $mission_id) . $key . $k;
					$plan_id = Db::name('plan')->insertGetId($data);
					$oids[] = $plan_id;
				} elseif ($v['type'] == 1) {
					$data['plan_form_no'] = get_order_sn('C', $mission_id) . $key . $k;
					$data['plan_oids'] = implode(",", $oids);
					$plan_id = Db::name('plan')->insertGetId($data);
				}
				unset($data);
			}
		}
		Db::name('user_card')->where(['card_id' => $cid])->update(['card_state' => 1]);
		Cache::rm($cacheName);

		//立即支付计划
		if ($payment['payment_paynow'] == '1') {
			$url = 'http://' . $_SERVER['HTTP_HOST'] . '/pay/' . $payment['payment_controller'] . '/pay_web?id=' . $mission_id;
			return json(['error' => '10', 'msg' => '计划生成成功', 'mid' => $mission_id, 'type' => 'pay_web', 'title' => '支付验证', 'url' => $url]);
		} else {
			//推送到队列
			controller("api/task")->index($mission_id);
		}
		return json(['error' => 0, 'msg' => '计划生成成功', 'mid' => $mission_id]);
	}


	/**
	 * 获取卡对应的支付通道
	 * @Author tw
	 * @Date   2018-09-20
	 * @return [type]     [description]
	 */
	public function getbankpayment()
	{
		$post = input('post.');
		if (empty($post)) {
			return json(['error' => 1, 'msg' => '非法请求']);
		}

		$uid = $this->uid;//用户id
		$cid = $post['cid'];//用户卡id

		//获取银行卡信息
		$card = Db::name('user_card')->where(['card_id' => $cid, 'card_type' => 1])->find();
		if (empty($card)) {
			return json(['error' => 1, 'msg' => '银行卡不存在']);
		}

		//获取可用支付渠道
		$channel_id = Db::name('payment_channel')->where(['channel_use' => 1])->order('channel_id')->column('channel_id');
		if (empty($channel_id)) {
			return json(['error' => 1, 'msg' => '无可用通道']);
		}
		$payment = Db::name('payment')->field('payment_id,payment_name,payment_bind,payment_region,payment_mcc')->where('payment_use', 'eq', '1')->where('payment_type', 'eq', '2')->whereIn('payment_channel_id', $channel_id)->select();
		if (empty($payment)) {
			return json(['error' => 1, 'msg' => '无可用通道']);
		}
		foreach ($payment as $k => $v) {
			//查询通道支持银行
			$bank = Db::name('bank')->where(['bank_bid' => $card['card_bank_id'], 'bank_pay_id' => $v['payment_id']])->find();
			if (empty($bank)) {
				unset($payment[$k]);
				continue;
			}

			if ($v['payment_bind'] == 0) {
				$payment[$k]['state'] = 1;
			} elseif ($v['payment_bind'] == 1) {
				$payment_card = Db::name('payment_card')
					->where('card_pay_id', 'eq', $v['payment_id'])
					->where('card_pay_uid', 'eq', $uid)
					->where('card_cid', 'eq', $cid)
					->where('card_type', 'eq', '1')
					->where('card_state', 'eq', '1')
					->find();
				if (empty($payment_card)) {
					$payment[$k]['state'] = 0;
				} else {
					$payment[$k]['state'] = 1;
				}
			}
		}
		return json(['error' => 0, 'msg' => '成功', 'data' => $payment]);

	}

	/*
	 * 根据笔数总金额生成计划
	 * $num 笔数
	 * $money 共计金额
	 * $min 最小
	 * $max 最大
	 * $riskStart 风控值
	 * $riskEnd 风控值
	 * $dengcha 平均数
	 * 2018年9月18日23:18:50
	 * 刘媛媛
	 */


	public function dispersed($num, $money, $min, $max, $riskStart, $riskEnd, $dengcha)
	{

		/*
		980-242/3 = 246
		980-242-246/2 = 246
		980-242-246-246 = 260
		*/
		$Newsrisk = intval($riskEnd / $num);
		$list = array();
		$count = 0;
		for ($n = 0; $n < $num; $n++) {
			if ($n == 0) {
				$list[] = round($dengcha + (rand(1, 100) * 0.01) - rand($riskStart, $Newsrisk), 2);
			} else {
				$cha = 0;
				foreach ($list as $value) {
					$cha += $value;
				}

				//echo $cha;
				//echo '<br/>';
				if (($num - 1) == $n) {
					$nodess = 0;
					$nodess = $money - $cha;
					$tpl = $nodess;
					if ($max != 0) {

						if ($nodess > $max) {
							$nodess = $max - (rand(1, 100) * 0.01);
						}

						sort($list);

						$list[0] = round($tpl - $nodess + $list[0], 2);

					}
					$list[] = round($nodess, 2);
				} else {
					$nodes = intval(($money - $cha) / ($num - $n)) + (rand(1, 100) * 0.01) - rand($riskStart, $Newsrisk);
					if ($min != 0) {
						if ($nodes < $min) {
							$nodes = $min + (rand(1, 100) * 0.01) + rand($riskStart, $Newsrisk);
						}

					}

					if ($max != 0) {
						if ($nodes > $max) {
							$nodes = $max + (rand(1, 100) * 0.01) - rand($riskStart, $Newsrisk);
						}
					}
					$list[] = round($nodes, 2);
					unset($nodes);

				}

			}

		}

		$sum = 0;
		$isdg = false;
		foreach ($list as $vsa) {
			$sum += $vsa;
			if ($max > 0) {
				if ($vsa > $max) {
					$isdg = true;
				}
			}
			if ($min > 0) {
				if ($vsa < $min) {
					$isdg = true;
				}
			}

		}
		if ($isdg) {
			return $this->dispersed($num, $money, $min, $max, $riskStart, $riskEnd, $dengcha);
		}
		return $list;
	}

	//生成随机时间
	function rand_time($start_time, $end_time)
	{
		$start_time = strtotime($start_time);
		$end_time = strtotime($end_time);
		return date('Y-m-d H:i:s', mt_rand($start_time, $end_time));
	}

	/**
	 * 通道查询
	 * @return [type] [description]
	 */
	public function getpayment()
	{
		if ($this->request->isPost()) {
			$post = input('post.');
			if (empty($post)) {
				return json(['error' => 1, 'msg' => '参数错误']);
			}
			$pay_id = $post['pay_id'];
			$type = isset($post['type']) ? $post['type'] : 1;
			//获取支付通道配置信息
			$payment = Db::name('payment')
				->field('payment_day_num,payment_num,payment_min_money,payment_max_money,payment_risk_start,payment_risk_end,paymentst_entime,paymentst_money,payment_pattern,payment_region,payment_mcc')
				->where('payment_id', $pay_id)
				->where('payment_type', $type)
				->where('payment_use', 1)
				->find();
			$payment["payment_min_money"] = $payment["payment_min_money"];
			$payment["payment_max_money"] = ($payment["payment_max_money"] % $payment["payment_pattern"] > 0) ? $payment["payment_max_money"] - ($payment["payment_max_money"] % 1000) : $payment["payment_max_money"];
			if ($pay_id == "2") {
				$payment["payment_min_money"] = "100.00";
				$payment["payment_max_money"] = "1000.00";
			}
			if ($payment) {
				return json(['error' => 0, 'msg' => '查询成功', 'data' => $payment]);
			} else {
				return json(['error' => 1, 'msg' => '渠道通道不存在']);
			}
		} else {
			return json(['error' => 1, 'msg' => '非法请求']);
		}

	}

	/*
     * 获取会员升级渠道
     * 2018年9月18日23:48:52
     * 刘媛媛
     */
	public function MembergetChannel()
	{
		if ($this->request->isPost()) {
			$post = input('post.');
			if (empty($post)) {
				return json(['error' => 1, 'msg' => '参数错误']);
			}
			$list = Db::name('paymentChannel')->where('channel_use', 1)->select();
			$type = isset($post['type']) ? $post['type'] : 4;
			foreach ($list as $key => $vs) {
				$payment = Db::name('payment')
					->where('payment_channel_id', $vs['channel_id'])
					->where('payment_type', $type)
					->where('payment_use', 1)->find();
				if (empty($payment)) {
					unset($list[$key]);
				}
			}
			$list = array_values($list);
			if (!$list) {
				return json(['error' => 1, 'msg' => '此平台未配置渠道']);
			} else {
				return json(['error' => 0, 'msg' => '成功', 'data' => $list]);
			}

		} else {
			return json(['error' => 1, 'msg' => '参数错误']);
		}
	}

	/**
	 * 获取行业编码
	 * @Author tw
	 * @Date   2018-09-26
	 * @return [type]     [description]
	 */
	public function get_mcc()
	{
		$post = input('post.');
		$pay_id = $post['pay_id'];
		$uid = $this->uid;
		$cid = $post['cid'];
		$region = $post['region'];
		$city_id = $post['city_id'];
		$payment = Db::name('payment')
			->where('payment_id', $pay_id)
			->where('payment_type', 2)
			->where('payment_use', 1)
			->find();
		if (empty($payment)) {
			return json(['error' => 1, 'msg' => '请选择支付通道']);
		}
		if ($payment['payment_mcc'] == 1) {
			if ($city_id) {
				$list = controller('pay/' . $payment['payment_controller'])->mcc($pay_id, $uid, $cid, $city_id);
			} else {
				$region_arr = explode('-', $region);
				$list = controller('pay/' . $payment['payment_controller'])->query_city_mcc($pay_id, $uid, $cid, $region_arr[0], $region_arr[1]);
			}
		} else {
			$list = Db::name('payment_mcc')
				->field('mcc_mcc as mcc,mcc_title as name')
				//->where('mcc_pay_id', $pay_id)
				->select();
			$list = ['error' => 0, 'msg' => '成功', 'data' => $list];
		}
		return json(['error' => 0, 'msg' => '成功', 'data' => $list]);
	}

	/**
	 * 还款详情
	 * @Author tw
	 * @Date   2018-09-29
	 * @return [type]     [description]
	 */
	public function detail()
	{
		$getdata = array();
		$post = input('post.');
		if (empty($post)) {
			return json(['error' => 1, 'msg' => '参数错误']);
		}
		$uid = $this->uid;
		$mid = $post['mid'];

		$mission = Db::name('mission')->where('mission_id', $mid)->where('mission_uid', $uid)->find();
		if (empty($mission)) {
			return json(['error' => 1, 'msg' => '计划不存在']);
		}
		if ($mission['mission_state'] == 1) {
			switch ($mission['mission_type']) {
				case '1':
				case '2':
					$type_name = '失败';
					break;
				case '3':
					$type_name = '还款中';
					break;
				default:
					$type_name = '待还';
					break;
			}
			$mission['type_name'] = $type_name;
		} elseif ($mission['mission_state'] == 2) {
			$mission['type_name'] = '已还完';
		} elseif ($mission['mission_state'] == 3) {
			$mission['type_name'] = '失败';
		} elseif ($mission['mission_state'] == 4) {
			$mission['type_name'] = '用户终止';
		}
		$card = Db::name('user_card')->alias('c')->join('bank_list uc', 'uc.list_id=c.card_bank_id', 'LEFT')->where('card_id', $mission['mission_cid'])->where('card_uid', $uid)->where('card_type', 1)->find();
		if (empty($card)) {
			return json(['error' => 1, 'msg' => '银行卡不存在']);
		}
		$plan = Db::name('plan')->field('plan_id,plan_uid,plan_mid,plan_form_no,plan_money,plan_type,plan_state,plan_pay_time,plan_oids,plan_fee,plan_mcc,plan_mcc_name')->where('plan_uid', $uid)->where('plan_mid', $mid)->where('plan_type', 1)->order('plan_sort asc ,plan_pay_time asc')
			->paginate('10', false, ['query' => $getdata]);
		if ($plan->toArray()) {
			//0未启动 1还款中 2已还完 3还款失败
			foreach ($plan as $k => $v) {
				$data = array();
				// $data = $v;

				$kk = Db::name('plan')->field('plan_id,plan_uid,plan_mid,plan_form_no,plan_money,plan_type,plan_state,plan_pay_time,plan_oids,plan_fee,plan_fee2,plan_mcc,plan_mcc_name')->where('plan_id', 'in', $v['plan_oids'])->select();
				array_push($kk, $v);

				foreach ($kk as $key => $value) {
					$data[$key] = $value;
					if ($value['plan_type'] == 1) {
						$type = '还款';

						if ($value['plan_state'] == 0) {
							$status = '待还';
						} else if ($value['plan_state'] == 1) {
							$status = '已还';
						} else if ($value['plan_state'] == 2) {
							$status = '失败';
						} else if ($value['plan_state'] == 3) {
							$status = '还款中';
						}
						$data[$key]['plan_money'] = (string)($value['plan_money']);
					} else if ($value['plan_type'] == 2) {
						$type = '消费';
						if ($value['plan_state'] == 0) {
							$status = '待消费';
						} else if ($value['plan_state'] == 1) {
							$status = '消费';
						} else if ($value['plan_state'] == 2) {
							$status = '失败';
						} else if ($value['plan_state'] == 3) {
							$status = '支付中';
						}
						$data[$key]['plan_money'] = (string)($value['plan_money'] + $value['plan_fee'] + $value['plan_fee2']);
					}

					$data[$key]['status'] = $status;
					$data[$key]['type_name'] = $type;

				}
				$plan->offsetSet($k, $data);
			}
		}
		$money = Db::name('plan')->where('plan_uid', $uid)->where('plan_mid', $mid)->where('plan_type', 1)->where('plan_state', 1)->sum('plan_money');

		$bank['name'] = $card['list_name'];//银行名称
		$bank['no'] = substr($card['card_no'], -4, 4);//银行卡号
		$bank['logo'] = 'http://' . $_SERVER['HTTP_HOST'] . $card['list_logo'];//银行卡号
		// $plan['error'] = 0;
		// return json($plan);
		return json([
			'error' => 0,
			'msg' => '成功',
			'money' => $money,
			'bank' => $bank,
			'mission' => $mission,
			'data' => $plan
		]);
	}

	/**
	 * 关闭/终止计划
	 * @Author tw
	 * @Date   2018-09-29
	 * @return [type]     [description]
	 */
	public function close()
	{
		$post = input('post.');
		$mid = $post['mid'];
		$uid = $this->uid;

		$mission = Db::name('mission')->where('mission_id', $mid)->where('mission_uid', $uid)->where('mission_del', 0)->find();
		if (empty($mission)) {
			return json(['error' => 1, 'msg' => '计划不存在']);
		} elseif ($mission['mission_state'] == '4') {
			return json(['error' => 0, 'msg' => '计划已终止']);
		} elseif ($mission['mission_close'] == '1') {
			$up = Db::name('mission')->where('mission_id', $mid)->where('mission_uid', $uid)->where('mission_del', 0)->update(['mission_state' => 4]);
			if (empty($up)) {
				return json(['error' => 1, 'msg' => '操作失败请重试']);
			}
			Db::name('user_card')->where('card_id', $mission['mission_cid'])->update(['card_state' => 0]);
			return json(['error' => 0, 'msg' => '终止计划成功']);
		} elseif ($mission['mission_close'] == '2') {
			return json(['error' => 0, 'msg' => '终止计划申请已提交，本次还款成功后将终止该计划']);
		} else {
			$up = Db::name('mission')->where('mission_id', $mid)->where('mission_uid', $uid)->where('mission_del', 0)->update(['mission_close' => 2]);
			if (empty($up)) {
				return json(['error' => 1, 'msg' => '操作失败请重试']);
			}
			return json(['error' => 0, 'msg' => '提交终止计划申请成功,本次还款成功后将终止该计划']);
		}
	}

	/**
	 * 删除计划
	 * @Author tw
	 * @Date   2018-09-29
	 * @return [type]     [description]
	 */
	public function del()
	{
		$post = input('post.');
		$mid = $post['mid'];
		$uid = $this->uid;

		$mission = Db::name('mission')->where('mission_id', $mid)->where('mission_uid', $uid)->where('mission_del', 0)->find();
		if (empty($mission)) {
			return json(['error' => 1, 'msg' => '计划不存在']);
		} elseif ($mission['mission_del'] == '1') {
			return json(['error' => 0, 'msg' => '计划已成功删除']);
		}
		if ($mission['mission_close'] == 1) {
			$up = Db::name('mission')->where('mission_id', $mid)->where('mission_uid', $uid)->where('mission_del', 0)->update(['mission_state' => 3, 'mission_del' => 1]);
			if (empty($up)) {
				return json(['error' => 1, 'msg' => '操作失败请重试']);
			}
			Db::name('user_card')->where('card_id', $mission['mission_cid'])->update(['card_state' => 0]);
			return json(['error' => 0, 'msg' => '计划已成功删除']);
		} else {
			$up = Db::name('mission')->where('mission_id', $mid)->where('mission_uid', $uid)->where('mission_del', 0)->update(['mission_close' => 3]);
			if (empty($up)) {
				return json(['error' => 1, 'msg' => '操作失败请重试']);
			}
			return json(['error' => 0, 'msg' => '提交删除计划申请,本次还款成功后将删除该计划']);
		}

	}


	/**
	 * 商户注册
	 * @Author tw
	 * @Date   2018-10-11
	 * @return [type]     [description]
	 */
	public function register()
	{
		$post = input('post.');
		if (empty($post)) {
			return json(['error' => 1, 'msg' => '参数错误']);
		}
		$pay_id = $post['pay_id'];//支付通道id
		$uid = $this->uid;//用户id

		$payment_controller = Db::name('payment')->where('payment_id', $pay_id)->value('payment_controller');
		if (empty($payment_controller)) {
			return ['error' => 1, 'msg' => '通道不存在'];
		}
		$result = controller('pay/' . $payment_controller)->register($pay_id, $uid);
		return json($result);
	}


	/**
	 * 绑卡
	 */
	public function bind_web()
	{
		$get = input('get.');
		$pay_id = $get['pay_id'];
		$uid = $get['uid'];
		$cid = $get['cid'];
		$payment = Db::name('payment')->where('payment_id', $pay_id)->find();
		if (empty($payment)) {
			echo "<script>alert('通道不存在');window.history.back();</script>";
			exit;
			//return json(['error' => 1, 'msg' => '通道不存在']);
		}
		$payment_card = Db::name('payment_card')
			->where('card_uid', 'eq', $uid)
			->where('card_pay_id', 'eq', $pay_id)
			->where('card_cid', 'eq', $cid)
			->where('card_type', 'eq', '1')
			->where('card_state', 'eq', '1')
			->find();
		if ($payment_card) {
			//echo "<script>alert('已绑卡');window.history.back();</script>";exit;
			//return json(['error' => 1, 'msg' => '已绑卡']);
		}
		$result = controller('pay/' . $payment['payment_controller'])->bind_card($pay_id, $uid, $cid, 1);

		if (empty($result)) {
			echo "<script>alert('参数错误');window.history.back();</script>";
			exit;
			//return json(['error' => 1, 'msg' => '参数错误']);
		} elseif ($result['error'] == 0 && empty($result["returnUrl"])) {
			echo "<script>alert('" . $result["msg"] . "');window.history.back();</script>";
			exit;
		} elseif ($result['returnUrl']) {
			$url = $result['returnUrl'];
			echo "<script>alert('" . $result["msg"] . "');window.location.href='" . $url . "'</script>";
			exit;
		} else {
			echo "<script>alert('" . $result["msg"] . "');window.history.back();</script>";
			exit;
			//return json($result);
		}
	}

	/**
	 * 绑卡
	 * @Author tw
	 * @Date   2018-09-21
	 */
	public function bind_api()
	{
		$post = input('post.');
		if (empty($post)) {
			return json(['error' => 1, 'msg' => '参数错误']);
		}
		$type = $post['type'];//1 绑卡提交 2提交绑卡验证 3重新获取验证码
		$pay_id = $post['pay_id'];//支付通道id
		$cid = $post['cid'];//银行卡id
		$uid = $this->uid;//用户id
		$smscode = $post['smscode'];//验证码紧type为2时有用

		$payment_controller = Db::name('payment')->where('payment_id', $pay_id)->value('payment_controller');
		if (empty($payment_controller)) {
			return json(['error' => 1, 'msg' => '通道不存在']);
		}
		if ($type == 1) {
			//绑卡提交
			$result = controller('pay/' . $payment_controller)->bind_card($pay_id, $uid, $cid);
		} elseif ($type == 2) {
			if (empty($smscode)) {
				return json(['error' => 1, 'msg' => '请填写验证码']);
			}
			//绑卡验证
			$result = controller('pay/' . $payment_controller)->bind_smscode($pay_id, $uid, $cid, $smscode);

		} elseif ($type == 3) {
			//重发验证码
			$result = controller('pay/' . $payment_controller)->bind_retry_smscode($pay_id, $uid, $cid);

		}

		if (empty($result)) {
			$result = ['error' => 1, 'msg' => '参数错误'];
		}

		if(!isset($result["is_sms"])){
			$result["is_sms"] = 0;
		}
		return json($result);
	}

	/**
	 * 查询状态
	 * @Author tw
	 * @Date   2018-10-20
	 * @return [type]     [description]
	 */
	public function state_query()
	{
		$post = input('post.');
		$id = $post['id'];//订单id
		$uid = $post['uid'];//用户id
		$pay_id = $post['pay_id'];//通道id
		$form_no = $post['form_no'];//订单编号
		$up_no = $post['up_no'];//上游返回编号
		$date = $post['date'];//交易日期

		$payment_controller = Db::name('payment')->where('payment_id', $pay_id)->value('payment_controller');
		if (empty($payment_controller)) {
			return json(['error' => 1, 'msg' => '通道不存在']);
		}
		$result = controller('pay/' . $payment_controller)->state_query($id, $uid, $pay_id, $form_no, $up_no, $date);
		return json($result);
	}
}
 